package com.sophimp.jumphack;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Created by chenliang on 2017/12/31.
 */
public class BookFinder extends BaseTargetFinder {

    /**
     * 相对于人的位置左右加减
     */
    public final static int SCAN_RANGE_X = 320;

    /**
     * 相对于人的位置
     */
    public final static int SCAN_RANGE_Y = 160;

    public BookFinder() {
        // 中心圆边颜色
//        R_TARGET = 220;
//        G_TARGET = 225;
//        B_TARGET = 110;
        R_TARGET = 240;
        G_TARGET = 255;
        B_TARGET = 255;
    }

    public int[] find(BufferedImage image, int myPos[]) {
        if (image == null) {
            return new int[]{0, 0};
        }
        int width = image.getWidth();
        int height = image.getHeight();

        int rangeXMin = Math.max(0, myPos[0] - SCAN_RANGE_X);
        int rangeXMax = Math.min(1080, myPos[0] + SCAN_RANGE_X);
        int rangeYMin = myPos[1] - SCAN_RANGE_Y;
        int rangeYMax = myPos[1] - 15;
        int[] ret = {0, 0};
        int maxX = Integer.MIN_VALUE;
        int minX = Integer.MAX_VALUE;
        int maxY = -1;
        int minY = -1;
        // 找脚后跟, 高度 20px
        // 从右向左找
        for (int i = rangeXMin; i <= rangeXMax; i++) {
            for (int j = rangeYMax; j >= rangeYMin; j--) {
                int pixel = image.getRGB(i, j);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8;
                int b = (pixel & 0xff);
                if (ToleranceHelper.match(r, g, b, R_TARGET, G_TARGET, B_TARGET, 16)) {
//                    maxX = Integer.max(maxX, i);
//                    minX = Integer.min(minX, i);
//                    if (maxY == -1) {
//                        maxY = j;
//                        minY = maxY - 10;
//                    }
                    ret[0] = i;
                    ret[1] = j;
                    System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
                    return ret;
                }
            }
        }

        ret[0] = (maxX + minX) / 2;
        ret[1] = minY;
        System.out.println(maxX + ", " + minX);
        System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
        return ret;
    }

    public static void main(String... strings) throws IOException {
        MyPosFinder2 myPosFinder = new MyPosFinder2();
        BookFinder circleFinder = new BookFinder();
        String root = myPosFinder.getClass().getResource("/").getPath();
        System.out.println("root: " + root);
        String imgsSrc = root + "img/";
        String imgsDesc = root + "img/my_pos";
        File srcDir = new File(imgsSrc);
        System.out.println(srcDir);
        long cost = 0;
        File[] listFile = srcDir.listFiles();
        if (listFile == null || listFile.length == 0) return;
        for (File file : listFile) {
            if (!file.getName().endsWith(".png")) {
                continue;
            }
            System.out.println(file);
            BufferedImage img = ImgLoader.load(file.getAbsolutePath());
            long t1 = System.nanoTime();
            int[] myPos, circlePos;
            myPos = myPosFinder.find(img);
            long t2 = System.nanoTime();
            cost += (t2 - t1);
            BufferedImage desc = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics graphics = desc.getGraphics();
            graphics.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null); // 绘制缩小后的图

            // 画人物位置
            graphics.setColor(Color.RED);
            graphics.fillRect(myPos[0] - 5, myPos[1] - 5, 10, 10);

            circleFinder.updateYRange(myPos[1] - 250, myPos[1] - 100);
            if (file.getName().contains("left")) {
                circlePos = circleFinder.findRight(img);
            } else {
                circlePos = circleFinder.findLeft(img);
            }

            graphics.setColor(Color.BLUE);
            graphics.fillRect(circlePos[0] - 5, circlePos[1] - 5, 10, 10);


            File descFile = new File(imgsDesc, file.getName());
            if (!descFile.exists()) {
                descFile.mkdirs();
                descFile.createNewFile();
            }
            ImageIO.write(desc, "png", descFile);
        }
        System.out.println("avg time cost: " + (cost / listFile.length / 1_000_000));

    }
}
